Skip to content

Tighten LimitArrayPoolWriteStream cleanup contract#128328

Open
Copilot wants to merge 5 commits into
mainfrom
copilot/implement-suggestion-from-issue-comment
Open

Tighten LimitArrayPoolWriteStream cleanup contract#128328
Copilot wants to merge 5 commits into
mainfrom
copilot/implement-suggestion-from-issue-comment

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 18, 2026

Make LimitArrayPoolWriteStream.Dispose unconditionally throw.
This is an internal type where we always control the lifetime. The user should never be disposing it.
See #128083 (comment)

Copilot AI and others added 5 commits May 18, 2026 12:56
…fers directly

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/67c929c4-11bf-4536-bd71-7f640ddcfb01

Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
…try/finally

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/67c929c4-11bf-4536-bd71-7f640ddcfb01

Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
…ionException

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/648b000d-d351-4916-bf8c-13142b1ebff1

Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
…PoolWriteStream

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/2bc5c1de-f6bd-40ff-9f88-efbc27d10f7e

Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 18, 2026 14:31
Copilot AI review requested due to automatic review settings May 18, 2026 14:31
@MihaZupan MihaZupan added this to the 11.0.0 milestone May 18, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

@MihaZupan MihaZupan marked this pull request as ready for review May 18, 2026 14:34
Copilot AI review requested due to automatic review settings May 18, 2026 14:34
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Tightens the cleanup contract for the internal LimitArrayPoolWriteStream used by HttpContent buffering. Previously Dispose quietly returned pooled buffers, which meant a user-supplied SerializeToStream(Async) override that disposed the passed-in stream could silently corrupt the buffered content / ArrayPool.Shared. After this PR, Dispose is reserved for "the user did something wrong" and throws, while all framework-side cleanup goes through the new internal ReturnAllPooledBuffers() directly.

Changes:

  • LimitArrayPoolWriteStream.Dispose(bool) now throws InvalidOperationException (with explanatory comment); ReturnAllPooledBuffers is promoted from private to internal.
  • All framework callsites (HttpContent.LoadIntoBuffer[Async], LoadIntoBufferAsyncCore, HttpContent.Dispose, HttpClient.GetStringAsyncCore / GetByteArrayAsyncCore) replace Dispose() / using with explicit ReturnAllPooledBuffers() calls in finally/catch.
  • New unit test verifies that disposing the buffered stream from a user SerializeToStreamAsync override surfaces InvalidOperationException out of LoadIntoBufferAsync; StreamToStreamCopyTest cleanup is simplified to best-effort.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs Switches internal cleanup paths to ReturnAllPooledBuffers, exposes it internal, and makes Dispose throw to catch user misuse.
src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.cs Replaces using var buffer = ... patterns in GetStringAsyncCore / GetByteArrayAsyncCore with nullable locals and explicit ReturnAllPooledBuffers() in finally.
src/libraries/System.Net.Http/tests/UnitTests/HttpContentTest.cs Adds regression test asserting InvalidOperationException when a user SerializeToStreamAsync disposes the buffered stream.
src/libraries/System.Net.Http/tests/UnitTests/StreamToStreamCopyTest.cs Drops using on LimitArrayPoolWriteStream instances (now invalid) and inserts best-effort ReturnAllPooledBuffers() calls in the resizing test.

Comment thread src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs
Comment thread src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants